import config from './config.js';
export default {
	request (options = {}) {
		return new Promise((resolve, reject) => {
			const token = uni.getStorageSync("token")
			if (token) {
				options.header.Authorization = `Bearer ${token}`;
			}
			options.url = config.baseUrl + options.url
			options.complete = (response) => {
				if (response.statusCode == 200 || response.statusCode == 0) {
					if (response.data.Code == 401) {
						uni.reLaunch({
							url: "/pages/index/noLogin"
						})
						uni.removeStorageSync('userInfo');
					}
					// if (response.data.Code == 500 || response.data.Code == 0) {
					// 	uni.showToast({title:response.data.msg})
					// }
					resolve(response.data)
				} else {
					uni.showToast({
						icon: "none",
						title: response.data?.msg || '接口异常'
					})
				}
			}

			uni.request(options)
		})
	},

	post (url, data = {}, header = {}) {
		let options = {
			url: url,
			data: data,
			header,
			method: "POST"
		}
		return this.request(options);
	},
	delete (url, data = {}, header = {}) {
		let options = {
			url: url,
			data: data,
			header,
			method: "DELETE"
		}
		return this.request(options);
	},
	put (url, data = {}, header = {}) {
		let options = {
			url: url,
			data: data,
			header,
			method: "PUT"
		}
		return this.request(options);
	},
	get (url, data = {}, header = {}) {
		let options = {
			url: url,
			data: data,
			header: header
		}
		return this.request(options);
	}
};